var MesDetailsDeCommande = {
    
    init: function(Fieldset_detail_commande) {
        
        /***********************************************************************
         * Variables
         **********************************************************************/
        var LinkAdd = Fieldset_detail_commande.find('a.addline'),
            TableDetailCommande = Fieldset_detail_commande.find('table#liste_details_commande');
        
        /***********************************************************************
         * Methods on events
         **********************************************************************/
        function OnAdd(event, element) {
            event.preventDefault();
            
            var tr = '<tr>';
            tr = tr + '<td><input type="text" name="ARTCILE_LIBELLE" required="required" /><input type="hidden" name="ARTICLE_ID" required="required" /></td>';
            tr = tr + '<td><input type="text" name="DETAIL_QUANTITE" required="required" value="0" /></td>';
            tr = tr + '</tr>';
            
            TableDetailCommande.find('thead').after(tr);
            
            var newLine = TableDetailCommande.find('tr').not('thead tr').first(),
                ARTCILE_LIBELLE = newLine.find('input[type=text][name="ARTCILE_LIBELLE"]'),
                ARTICLE_ID = newLine.find('input[type=hidden][name="ARTICLE_ID"]'),
                DETAIL_QUANTITE = newLine.find('input[type=text][name="DETAIL_QUANTITE"]');
            
            var base_url = $('input[type=hidden]#base_url').val(),
                url_Autocomplete = base_url + 'matiere_premiere/GetList_Autotcomplete';
            
            ARTCILE_LIBELLE.autocomplete({
                source: function (request, response) {
                    $.ajax({
                        type: "GET",
                        url: url_Autocomplete,
                        data: {
                            featureClass: 'P',
                            style: 'full',
                            maxRows: 8,
                            name_startsWith: request.term
                        },
                        async: false,
                        dataType: 'json',
                        success: function(Results){
                            response($.map(Results, function(Result) {
                                return { label: Result.Label, value: Result.Value, id: Result.ID };
                            }));
                        }
                    });
                },
                minLength: 2,
                select: function(event, ui) {
                    
                    var AbleToAdd = true;
                    var EveryARTICLE_ID = $('input[type=hidden][name="ARTICLE_ID"]');
                    $.each(EveryARTICLE_ID, function(index, input) {
                        if($(input).val() == ui.item.id) {
                            AbleToAdd = false;
                        }
                    })
                    
                    switch (AbleToAdd) {
                        case true:
                            ARTICLE_ID.val(ui.item.id);
                            return true;
                        break;
                        
                        case false:
                            alert('Cet article a déjà été ajouté !');
                            return false;
                        break;
                    }
                }
            });
            
            DETAIL_QUANTITE.spinner({
                min: 0
            });
        }
        
        /***********************************************************************
         * Events
         **********************************************************************/
        LinkAdd.on('click', function(event) {
            OnAdd(event, $(this));
        });
        
    }
    
};